package com.huafv.flyauth.web.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huafv.flyauth.core.domain.Result;
import com.huafv.flyauth.core.vo.DictTypeListVo;
import com.huafv.flyauth.utils.BeanCopyUtils;
import com.huafv.flyauth.web.admin.entity.PageBean;
import com.huafv.flyauth.web.admin.entity.SysDictType;
import com.huafv.flyauth.web.admin.entity.SysDictTypePage;
import com.huafv.flyauth.web.admin.mapper.SysDictTypeMapper;
import com.huafv.flyauth.web.admin.service.SysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * (SysDictType)表服务实现类
 *
 * @author makejava
 * @since 2023-05-18 16:40:28
 */
@Service
@SuppressWarnings("all")
public class SysDictTypeServiceImpl extends ServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {
    @Autowired
    private SysDictTypeMapper sysDictTypeMapper;
    @Override
    public Result dictTypeList(Integer pageNum, Integer pageSize) {
        // todo 默认分页值 或者 不传分页参数 返回错误码要求传参

        //开启分页查询
        PageHelper.startPage(pageNum,pageSize);
        //查询出来的字典类型状态必须为0
        List<SysDictType> list = sysDictTypeMapper.dictTypeList();
        //将list强转为page对象
        Page p = (Page)list;
        List<SysDictType> resultList = p.getResult();
        //将列表数据转换为vo对象进行响应
        List<DictTypeListVo> result = resultList
                .stream()
                .map(sysDictType ->{
                        DictTypeListVo vo;
                        try {
                            vo = BeanCopyUtils.beanCopy(sysDictType,DictTypeListVo.class);
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                        return vo;
                })
                .collect(Collectors.toList());
        PageBean pageBean = new PageBean(p.getTotal(),result);
        return new Result().success(pageBean);
    }

    /*
    * 新增字典类型
    * */
    @Override
    public Result insertDictType(SysDictType sysDictType) {
        //将创建时间设为当前时间
        sysDictType.setCreateTime(LocalDateTime.now());
        sysDictType.setUpdateTime(LocalDateTime.now());
        int row = sysDictTypeMapper.insertDictType(sysDictType);
        if(row > 0){
            return new Result().success();
        }
        return new Result().error("新增失败");
    }


    @Override
    public Result getByCondition(SysDictTypePage sysDictTypePage) {
        //开启分页查询
        PageHelper.startPage(sysDictTypePage.getPageNum(),sysDictTypePage.getPageSize());
        List<SysDictType> list = sysDictTypeMapper.getByCondition(sysDictTypePage);
        //获取分页查询的数据
        Page p = (Page) list;
        List<SysDictType> resultList = p.getResult();
        //进行vo转换
        List<DictTypeListVo> result = resultList
                .stream()
                .map(sysDictType -> {
                    DictTypeListVo dictTypeListVo;
                    try {
                        dictTypeListVo = BeanCopyUtils.beanCopy(sysDictType, DictTypeListVo.class);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                    return dictTypeListVo;
                })
                .collect(Collectors.toList());
        PageBean pageBean = new PageBean(p.getTotal(),result);
        return Result.success(pageBean);
    }

    @Override
    public Result deleteDictTypeByIds(Integer[] ids) {
        sysDictTypeMapper.deleteDictTypeByIds(ids);

        //ToDo 删除字典键值的数据

        return Result.success();
    }


}